//
//  gxdy_ContactPropertyCell.m
//  OCQ_API_Test
//
//  Created by 故乡的云  on 2017/5/2.
//  Copyright © 2017年 故乡的云 . All rights reserved.
//
#pragma mark - ----- 联系人属性cell -----


#import "gxdy_ContactPropertyCell.h"
#import "gxdy_ContactPropertyModel.h"
#import "Gxdy_ContactModel.h"

typedef enum : NSUInteger {
    ContactPropertyCellTypeText, // 纯文本类型
    ContactPropertyCellTypeReference, // 可引用提供数据
    ContactPropertyCellTypePhone, // 电话
    ContactPropertyCellTypeEmail, // 邮箱
    ContactPropertyCellTypeQQ, // qq
    ContactPropertyCellTypeWeChat, // 微信
    ContactPropertyCellTypeWeibo, // 微博
    ContactPropertyCellTypeDatetime, // 日期时间类型
    ContactPropertyCellTypeTextarea // 多行纯文本：备注
} ContactPropertyCellType;

@interface gxdy_ContactPropertyCell ()<UITextFieldDelegate,UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UITextField *contentTextField;
@property (weak, nonatomic) IBOutlet UIButton *btn;
@property (weak, nonatomic) IBOutlet UITextView *textView;


/// contentTextField右边与父视图间的约束
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *contentTextFRightConstraint;

@property (nonatomic, readonly) ContactPropertyCellType cellType;
@end

@implementation gxdy_ContactPropertyCell
+ (instancetype)cell {
    return [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil][0];
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

#pragma mark - properties
- (void)setModel:(Gxdy_ContactModel *)model {
    NSString *key = self.contactModel.name;
    if ([key isEqualToString:@"description"]) {
        self.textView.text = model.remark;
        return;
    }else if([key isEqualToString:@"homePhone"]) {
        self.contentTextField.text = model.phone;
        return;
    }else if([key isEqualToString:@"lastContactedOn"]) {
        self.contentTextField.text = @"lastContactedOn";
        return;
    }
    self.contentTextField.text = [model valueForKey:key];
}

- (void)setContactModel:(gxdy_ContactPropertyModel *)contactModel {
    _contactModel = contactModel;
    self.titleLabel.text = contactModel.label;
    
//    gxdy_Log(@"%@type:%@",contactModel.label ,contactModel.type);

    self.contentTextField.enabled = contactModel.allowUpload;
    [self setCellType:contactModel.type];
}

- (void)setCellType:(NSString *)cellType {
    if ([cellType isEqualToString:@"text"]) {
        _cellType = ContactPropertyCellTypeText;
        
    }else if ([cellType isEqualToString:@"reference"]) {
        _cellType = ContactPropertyCellTypeReference;
        
    }else if ([cellType isEqualToString:@"phone"]) {
        _cellType = ContactPropertyCellTypePhone;
        
    }else if ([cellType isEqualToString:@"email"]) {
        _cellType = ContactPropertyCellTypeEmail;
        
    }else if ([cellType isEqualToString:@"qq"]) {
        _cellType = ContactPropertyCellTypeQQ;
        
    }else if ([cellType isEqualToString:@"wechat"]) {
        _cellType = ContactPropertyCellTypeWeChat;
        
    }else if ([cellType isEqualToString:@"weibo"]) {
        _cellType = ContactPropertyCellTypeWeibo;
        
    }else if ([cellType isEqualToString:@"datetime"]) {
        _cellType = ContactPropertyCellTypeDatetime;
        
    }else if ([cellType isEqualToString:@"textarea"]) {
        _cellType = ContactPropertyCellTypeTextarea;
    }
    
    [self setContentTextFieldKeyBoardType];
    [self layoutContentTextField];
}

#pragma mark 布局设置
/// 设置键盘类型（及returnKey类型）
- (void)setContentTextFieldKeyBoardType {
    
    // key board type
    if (self.cellType == ContactPropertyCellTypePhone || self.cellType == ContactPropertyCellTypeQQ || [self.contactModel.label isEqualToString:@"邮编"]) {
        self.contentTextField.keyboardType = UIKeyboardTypePhonePad;
    }else if (self.cellType == ContactPropertyCellTypeWeibo || self.cellType == ContactPropertyCellTypeEmail){
        self.contentTextField.keyboardType = UIKeyboardTypeEmailAddress;
    }else {
        self.contentTextField.keyboardType = UIKeyboardTypeDefault;
    }
    
    // returnKey （最好放在外面设置）
    if ([self.contactModel.label isEqualToString:@"备注"]) {
        self.textView.returnKeyType = UIReturnKeyDefault;
    }else {
        self.contentTextField.returnKeyType = UIReturnKeyDone; //UIReturnKeyNext;
    }
}

/// 显示或隐藏btn,更新ContentTextField布局
- (void)layoutContentTextField {
    if (self.cellType == ContactPropertyCellTypeText) {
        self.btn.hidden = YES;
        self.contentTextField.hidden = NO;
        self.textView.hidden = YES;
        self.contentTextFRightConstraint.constant = 10.0;
    }else if (self.cellType == ContactPropertyCellTypeTextarea) {
        self.btn.hidden = YES;
        self.contentTextField.hidden = YES;
        self.textView.hidden = NO;
        return;
    }else {
        self.contentTextFRightConstraint.constant = (10 + self.btn.bounds.size.width + 5.0);
        // 设置btn图片
        NSString *imageName = nil;
        if (self.cellType == ContactPropertyCellTypeReference) {
            imageName = @"iconfont-sousuo";
        }else if (self.cellType == ContactPropertyCellTypePhone) {
            imageName = @"iconfont-phone-blue";
        }else if (self.cellType == ContactPropertyCellTypeEmail) {
            imageName = @"iconfont-email-blue";
        }else if (self.cellType == ContactPropertyCellTypeQQ) {
            imageName = @"iconfont-qq-blue";
        }else if (self.cellType == ContactPropertyCellTypeWeChat) {
            imageName = @"iconfont-wechat-blue";
        }else if (self.cellType == ContactPropertyCellTypeWeibo) {
            imageName = @"iconfont-blog-blue";
        }
        [self.btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
        self.contentTextField.hidden = NO;
        self.textView.hidden = YES;
    }
}

#pragma mark - 事件处理
//- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
//    [super setHighlighted:highlighted animated:animated];
//    if (highlighted && self.contentTextField.enabled) {
//        [self.contentTextField becomeFirstResponder];
//    }
//}

- (IBAction)btnClick:(UIButton *)sender {
    if (self.cellType == ContactPropertyCellTypeReference) {
        
    }else if (self.cellType == ContactPropertyCellTypePhone) {
        
    }else if (self.cellType == ContactPropertyCellTypeEmail) {
        
    }else if (self.cellType == ContactPropertyCellTypeQQ) {
        
        
    }else if (self.cellType == ContactPropertyCellTypeWeChat) {
        
    }else if (self.cellType == ContactPropertyCellTypeWeibo) {
        
    }
}





#pragma mark - text field delegate && text view delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}


@end
